package fit.conceptmap.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import fit.conceptmap.beans.ConceptMapsBean;
import fit.conceptmap.beans.JsonBean;
import fit.conceptmap.beans.LinkDataArrayBean;
import fit.conceptmap.beans.NodeDataArrayBean;
import fit.conceptmap.entities.Link;
import fit.conceptmap.entities.Node;

public class DrawUtils {
	public static ConceptMapsBean parseJsonToConceptMap(JsonBean json) {
		ConceptMapsBean bean = new ConceptMapsBean();
		bean.setDigram(json.getDiagram());
		
		// Set value for NodeDataArrayBean
		List<NodeDataArrayBean> nodeDataArray = new ArrayList<NodeDataArrayBean>(); 
		NodeDataArrayBean nodeData = null;
		for (HashMap<String, Object> map : json.getNodeDataArray()) {
			nodeData = new NodeDataArrayBean();
			nodeData.setKey((int)map.get("key"));
			nodeData.setLoc(map.get("loc").toString());
			nodeData.setText(map.get("text").toString());
			nodeDataArray.add(nodeData);
		}
		bean.setNodeDataArray(nodeDataArray);
		
		// Set value for LinkDataArrayBean
		List<LinkDataArrayBean> linkDataArray = new ArrayList<LinkDataArrayBean>(); 
		LinkDataArrayBean linkData = null;
		for (HashMap<String, Object> map : json.getLinkDataArray()) {
			linkData = new LinkDataArrayBean();
			linkData.setFrom((int)map.get("from"));
			linkData.setTo((int)map.get("to"));
			linkData.setPoints(map.get("points").toString());
			if(null == map.get("text")) {
				linkData.setText("");
			} else {
				linkData.setText(map.get("text").toString());
			}
			linkDataArray.add(linkData);
		}
		bean.setLinkDataArray(linkDataArray);
		
		return bean;
	}
	
	public static String random() {
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		return (format.format(date) + " " + System.nanoTime()) ;
//		return String.valueOf(new Random().nextInt());
	}
	
	/**
	 * Set value from database for bean 
	 * @param conceptMap
	 * @param concepts
	 * @param nodes
	 * @param relationships
	 * @param links
	 * @return
	 */
	public static JsonBean parseConceptMapToJsonBean(List<Node> nodes, List<Link> links) { 
		JsonBean result = new JsonBean();
		result.setDiagram(Constants.DIAGRAM);

		// Set nodeDataArray
		List<HashMap<String, Object>> nodeDataArray = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> hashMapNode = null; 
		for (Node node : nodes) {
			hashMapNode = new HashMap<String, Object>();
			hashMapNode.put("key", Integer.valueOf(node.getNodeKey()));
			hashMapNode.put("loc", String.valueOf(node.getLoc()));
			hashMapNode.put("text", String.valueOf(node.getText()));
			nodeDataArray.add(hashMapNode);
		}
		result.setNodeDataArray(nodeDataArray);
		
		// Set linkDataArray
		List<HashMap<String, Object>> linkDataArray = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> hashMapLink = null; 
		for (Link link : links) {
			hashMapLink = new HashMap<String, Object>();
			hashMapLink.put("from", link.getLinkFrom());
			hashMapLink.put("to", link.getLinkTo());
			hashMapLink.put("points", link.getPoint());
			hashMapLink.put("text", link.getText());
			linkDataArray.add(hashMapLink);
		}	
		result.setLinkDataArray(linkDataArray);
		
		return result;
	}
}
